package com.itany.netclass.service.impl;

import com.itany.netclass.constant.ChapterConstant;
import com.itany.netclass.constant.ResourceConstant;
import com.itany.netclass.dao.ChapterMapper;
import com.itany.netclass.dao.ResourceMapper;
import com.itany.netclass.entity.Chapter;
import com.itany.netclass.entity.Resource;
import com.itany.netclass.entity.ResourceExample;
import com.itany.netclass.exception.NoQuanXianException;
import com.itany.netclass.exception.NullSringException;
import com.itany.netclass.exception.PDFException;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.ResourceService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:2021/10/17 14:15
 * Version:1.0-SNAPSHOT
 */
public class ResourceServiceImpl implements ResourceService {

    @Override
    public Resource findMp4ByChapterId(String chapterId) throws PDFException, NullSringException {
        ResourceMapper resourceMapper = (ResourceMapper) ObjectFactory.getObject("ResourceMapper");
        ResourceExample example = new ResourceExample();
        example.or().
                andChapterIdEqualTo(Integer.parseInt(chapterId));
        List<Resource> resources = resourceMapper.selectByExample(example);
        if (!resources.isEmpty()) {
            if (ResourceConstant.RECOURSE_1_TYPE_MP4.equals(resources.get(0).getFileType())) {
                return resources.get(0);
            } else {
                throw new PDFException("文件为pdf需要下载");
            }

        } else {
            throw new NullSringException("该章节暂无资源需要请添加");
        }
    }

    @Override
    public void uploadPDF(String chapterId, HttpSession session, ServletOutputStream outputStream) throws PDFException, NullSringException, IOException {
        ResourceMapper resourceMapper = (ResourceMapper) ObjectFactory.getObject("ResourceMapper");
        ResourceExample example = new ResourceExample();
        example.or().
                andChapterIdEqualTo(Integer.parseInt(chapterId));
        List<Resource> resources = resourceMapper.selectByExample(example);
        if (!resources.isEmpty()) {
            if (ResourceConstant.RECOURSE_2_TYPE_PDF.equals(resources.get(0).getFileType())) {
                try{
                    String path = session.getServletContext().getRealPath(resources.get(0).getPath());
                    FileInputStream in = null;
                    OutputStream out = null;
                    in = new FileInputStream(path);
                    byte[] b = new byte[10240];
                    int i;
                    while((i = in.read(b)) != -1){
                        outputStream.write(b,0,i);
                    }
                    outputStream.flush();
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    throw e;
                } catch (IOException e) {
                    throw e;
                }
            } else {
                throw new PDFException("文件不是pdf无法操作");
            }

        } else {
            throw new NullSringException("该章节下暂无资源需要请添加");
        }
    }

    @Override
    public List<Resource>  findResourceById(String resourceId) {
        ResourceMapper resourceMapper = (ResourceMapper) ObjectFactory.getObject("ResourceMapper");
        return resourceMapper.selectResourceById(resourceId);
    }

    @Override
    public List<Resource> findResourceByForm(String title, String nickname, String kaishitime, String jieshutime, String status, String fileType) throws NullSringException {
        ResourceMapper resourceMapper = (ResourceMapper) ObjectFactory.getObject("ResourceMapper");
       List<Resource> list= resourceMapper.selectResourceByForm(title,nickname,kaishitime,jieshutime,status,fileType);
       if(!list.isEmpty()){
           return list;
       }else {
           throw new NullSringException("未查询到符合条件的资源");
       }


    }

    @Override
    public List<Resource> findAllResource() throws NullSringException {
        ResourceMapper resourceMapper = (ResourceMapper) ObjectFactory.getObject("ResourceMapper");
       List<Resource> L=resourceMapper.selectAllResource();
       if(L.isEmpty()){
           throw new NullSringException("暂无任何资源");
       }else {
           return L;
       }
    }

    @Override
    public void yongResource(String resourceId, String status) throws Exception {
        ResourceMapper resourceMapper = (ResourceMapper) ObjectFactory.getObject("ResourceMapper");
        ChapterMapper chapterMapper = (ChapterMapper) ObjectFactory.getObject("ChapterMapper");
//        CommentMapper commentMapper = (CommentMapper) ObjectFactory.getObject("CommentMapper");
//        CommentService commentService= (CommentService) ObjectFactory.getObject("CommentServiceTarget");
        Resource resource = resourceMapper.selectByPrimaryKey(Integer.parseInt(resourceId));
        Chapter chapter = chapterMapper.selectByPrimaryKey(resource.getChapterId());
        if(ChapterConstant.CHAPTER_STATUS_JINGYONG.equals(""+chapter.getStatus())
                && ResourceConstant.RESOURCE_STATUS_QIYONG.equals(status)){
            throw new NoQuanXianException(chapter.getTitle()+"已经被禁用了，资源无法启用");
        }
        if(ResourceConstant.RESOURCE_STATUS_QIYONG.equals(status)){
            resource.setStatus(Integer.parseInt(ResourceConstant.RESOURCE_STATUS_QIYONG));
            resourceMapper.updateByPrimaryKeySelective(resource);
        }
        if(ResourceConstant.RESOURCE_STATUS_JINYONG.equals(status)){
            resource.setStatus(Integer.parseInt(ResourceConstant.RESOURCE_STATUS_JINYONG));
            resourceMapper.updateByPrimaryKeySelective(resource);
        }

        //以下方法为，资源禁用以后，其评论也被禁用，但因为一些原因先注释掉，不然评论审核有逻辑上的bug
//        CommentExample example=new CommentExample();
//        example.or().andResourceIdEqualTo(Integer.parseInt(resourceId));
//        List<Comment> comments = commentMapper.selectByExample(example);
//        for(Comment comment:comments){
//            commentService.yongCommentByComment(""+comment.getId(),status);
//        }
    }
}
